﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlphaBeatSoundEngine
{
    public class MusicPattern
    {
        public MusicPattern(TimedTone[] tones)
        {
            _tones = tones;
        }

        private TimedTone[] _tones;

        public int Length { get { return _tones.Length;} }
        public TimedTone[] Tones { get { return (TimedTone[])_tones.Clone(); } }

        public MusicPattern GetAdjustedPatternWithFundamental(Tone fundamental)
        {
            TimedTone[] adjustedTones = new TimedTone[_tones.Length];

            /*
             * C = 2 first note
             * E = 7 fundamental
             * diff = 5 = D
             * 
             * */

            int diffFundAndFirstNote = fundamental - _tones[0].Tone;

            if(diffFundAndFirstNote < 0){
                diffFundAndFirstNote += 12; //octave up
            }

            Tone diffTone = (Tone)diffFundAndFirstNote;

            for (int i = 0; i < _tones.Length; i++)
            {
                adjustedTones[i] = new TimedTone(_tones[i].Tone.AdjustFromTone(diffTone), _tones[i].ToneLenght, _tones[i].Style, _tones[i].TimeMarked);
            }
            return new MusicPattern(adjustedTones);
        }

        public override string ToString()
        {
            string tonesString = "";
            string sep = " ";
            foreach(TimedTone tone in _tones)
            {
                tonesString += tone.ToString() + sep;
            }
            tonesString = tonesString.Substring(0, tonesString.Length - sep.Length);

            return "[Size : "+_tones.Length +" - " + tonesString +"]";
        }

    }
}
